package com.mymvpproject.presenter;

import com.mymvpproject.base.User;
import com.mymvpproject.model.LoginModel;
import com.mymvpproject.model.LoginModelImpl;
import com.mymvpproject.view.LoginView;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 1 on 2017/5/16.
 */

public class LoginPresenter {
    private LoginView loginView;
    private LoginModel loginModel;

    public LoginPresenter(LoginView loginView) {
        this.loginView = loginView;
        loginModel = new LoginModelImpl();
    }

    public void login(){
        final User user = new User();

        user.setUsername(loginView.getUsername());
        user.setPassword(loginView.getPassword());
        loginView.showLoading();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                loginModel.login(user, new LoginModel.OnLoginListener() {
                    @Override
                    public void loginSuccess(final String msg) {
                        Observable.from(new String[]{msg})
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<String>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onNext(String s) {
                                        loginView.hideLoading();
                                        loginView.showSuccessMsg(s);
                                    }
                                });

                    }

                    @Override
                    public void LoginFaild(final String msg) {
                        Observable.from(new String[]{msg})
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<String>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onNext(String s) {
                                        loginView.showFailedMsg(s);
                                        loginView.hideLoading();
                                    }
                                });

                    }
                });
            }
        }).start();
    }

    public void clear(){
        loginView.clearEditText();
    }

}
